package com.cloudkinto.common.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Auther: fanting
 * @Date: 2018/11/8 10:50
 * @Description:
 */
public class DateUtils {
    /**
     * 比较两个时间的时间差，比较出时间差值，单位天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }


    public static String getGeneratorYYMMDDStr() {
        String result = "";
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
        String year = strNow[0];
        String month = strNow[1];
        String day = strNow[2];
        String yearStr = year.toString().substring(2);
        result = result + yearStr + month + day;
        return result;
    }


    public static String getGeneratorYYMMStr() {
        String result = "";
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
        String year = strNow[0];
        String month = strNow[1];
        String yearStr = year.toString().substring(2);
        result = result + yearStr + month;
        return result;
    }

    // 获得某天最大时间 2020-02-19 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获得某天最小时间 2020-02-17 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    //日期推移 向前或向后推移？天
    public static Date passingDate(String datSstr, int days) {
        Date date = DateUtils.stringFormatToDate(datSstr);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, days);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    //日期推移 向前或向后推移？天
    public static Date passingDate(Date date, int days) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, days);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    public static Date passingDateHour(Date date, int hours) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hours);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }


    /**
     *功能描述 日期推移 单位：月
     *@author zhujiale
     *@date 2022/3/10
     */
    public static Date passingDateMonth(Date date, int months) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, months);//把日期往后增加一月.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一月的结果
        return date;
    }

    /**
     *功能描述 日期推移 单位：年
     *@author zhujiale
     *@date 2022/3/10
     */
    public static Date passingDateYear(Date date, int years) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, years);//把日期往后增加一年.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一年的结果
        return date;
    }

    public static String dateFormatToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    public static String dateFormatToString(Date date,String pattern){
        if (pattern == null || "".equals(pattern)){
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(date);
        return dateStr;
    }
    public static  Date stringFormatToDate(String dateStr){
        return stringFormatToDate(dateStr,null);
    }

    public static Date stringFormatToDate(String dateStr,String pattern){
        if (pattern == null || "".equals(pattern)){
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static  Date stringFormatToDateHms(String dateStr){
        return stringFormatToDateHms(dateStr,null);
    }

    public static Date stringFormatToDateHms(String dateStr,String pattern){
        if (pattern == null || "".equals(pattern)){
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 获取时间所在月第一天和最后一天
     *
     * @param start_time
     * @param num        0-第一天  1-最后一天
     * @param month      >0 下个月   <0 上个月 =0 当前月
     * @return
     */
    public static Date getEndDayOrOneDay(Date start_time, int num, int month) {
        Calendar cale = Calendar.getInstance();
        if (num == 0) {
            // 获取前月的第一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, month + 0);
            cale.set(Calendar.DAY_OF_MONTH, 1);
        } else {
            // 获取前月的最后一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, month + 1);
            cale.set(Calendar.DAY_OF_MONTH, 0);
        }
        return cale.getTime();
    }


    public static  String getTodayYYMMStr(){
        return dateFormatToString(new Date(),"yyyyMM");
    }

}
